package com.sense.kukoo.biz_library.adapter.provider

import android.view.View
import com.alibaba.android.arouter.utils.TextUtils
import com.sense.kukoo.biz_library.R
import com.sense.kukoo.biz_library.adapter.MySubscriptionsAdapter
import com.sense.kukoo.common.repo.bean.BookItemBean
import com.sense.kukoo.common.repo.bean.LibrarySubscriptionBean
import com.sense.kukoo.common.repo.bean.MySubscriptionsBean
import com.sense.kukoo.common.utils.ApplicationContext
import com.sense.kukoo.common.wiget.MySubscriptionsItemView
import com.sense.kukoo.common.wiget.adapter.provider.MultiItemProvider
import com.sense.kukoo.common.wiget.adapter.viewholder.BaseViewHolder

class MySubscriptionsItemProvider(val listener: MySubscriptionsAdapter.MySubscriptionsItemEventListener):
    MultiItemProvider<MySubscriptionsBean>() {

    override fun getItemType(): Int = MySubscriptionsBean.TYPE_DEF

    override fun getItemLayoutId(): Int = com.sense.kukoo.common.R.layout.common_my_subscriptions_item_container

    override fun onBindViewHolder(viewHolder: BaseViewHolder, bean: MySubscriptionsBean) {
        val view = viewHolder.getView(com.sense.kukoo.common.R.id.item) as MySubscriptionsItemView
        val item: LibrarySubscriptionBean = bean.item
        //
        view.setChecked(bean.isSubscripted)
        //
        view.setIconUrl(item.book.coverUrl)
        //
        view.isShowRedDot(item.isShowRedDot())
        //
        //val name = if (TextUtils.isEmpty(item.alias)) item.book.name else item.book.alias
        view.setDes(item.book.name)
        //设置相对时间
        /*val time = System.currentTimeMillis() - (item.book.createdAtTimestamp ?: 0)
        var hour = time / 1000 / 60 / 60
        if (hour > 99){
            hour = 99
        }
        view.setMsg(ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.my_subscriptions_updated_hint, hour))*/
        //
        view.setListener(object :MySubscriptionsItemView.OnSubscriptionsChangedListener{
            override fun onChanged(isChecked: Boolean) {
                listener.onItemSubscripted(bean, isChecked, viewHolder.adapterPosition)
            }

            override fun onItemClick(v: View?) {
                listener.onItemClick(bean)
            }
        })
    }
}